<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/28
 * Time: 14:27
 */
namespace app\command\cron;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class runSkillQuestion extends Command{

    protected function configure()
    {
        $this->setName('runSkillQuestion')
            ->setDescription('执行获取技巧题目任务');
    }

    /**
     * 建议每隔五秒执行一次
     * @param Input $input
     * @param Output $output
     * @return int|null|void
     * @throws \think\db\exception\DbException
     */
    protected function execute(Input $input, Output $output)
    {
        set_time_limit(17200);
        ini_set('memory_limit','5024M');
        //获取哪个城市的
        $city = '杭州';
        $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjE3eGMuY29tIiwiYXVkIjoiMTIxN3hjLmNvbSIsImlhdCI6MTY4NjEyMzU3OSwibmJmIjoxNjg2MTIzNTc5LCJ1c2VyIjp7ImlkXyI6IjE0NDUzIiwidXNlcl9uYW1lIjoiXHU1ZmFlXHU0ZmUxXHU3NTI4XHU2MjM3IiwidXNlcl9waG9uZSI6IjE4ODUyMTIyNTI5IiwidXNlcl9hdmF0YXIiOiJodHRwczpcL1wvdGhpcmR3eC5xbG9nby5jblwvbW1vcGVuXC92aV8zMlwvUE9nRXdoNG1JSE80bmliSDBLbE1FQ05qakd4UVVxMjRaRWFHVDRwb0M2aWNSaWNjVkdLU3lYd2liY1BxNEJXbWlhSUd1RzFpY3d4YVFYNmdyQzlWZW1ab0o4cmdcLzEzMiIsInd4dXNlcl9pZCI6IjE2MDgwIiwid3h1c2VyX3VuaW9uaWQiOiJvQTB1UnM3OFhiWDl2clZEdm5taFVNdTFCcjhBIiwidHR1c2VyX2lkIjpudWxsLCJ0dHVzZXJfdW5pb25pZCI6bnVsbCwicm9sZV8iOiIwIiwibG9jYXRpb25fIjoiXHU2NzZkXHU1ZGRlIiwic3RhdHVzXyI6IjEiLCJhZGRfdGltZSI6IjIwMjMtMDYtMDcgMTU6Mzk6MzciLCJsb25naXR1ZGVfIjpudWxsLCJsYXRpdHVkZV8iOm51bGx9fQ.swqbifz48Is_FEXvPqJ3x5W2X2HMBIRdteuOQssBmK0';

        //删除技巧等级
        Db::name('skill_paper')
            ->where('city_',$city)
            ->delete();
        //删除技巧章节
        Db::name('skill_chapter')
            ->where('city_',$city)
            ->delete();
        //删除技巧题
        Db::name('skill_chapter_question')
            ->where('city_',$city)
            ->delete();

        $skill_param['city'] = $city;
        $skill_grade = http_post_json('https://api.1217xc.com/exam2Weixin/getSkillList', $skill_param);
        $skill_grade = json_decode($skill_grade, true)['data'];
        foreach ($skill_grade as $k=>$v){
            if($v['city_'] === '0'){
                unset($skill_grade[$k]);
            }
        }
        $skill_grade = array_values($skill_grade);
        foreach ($skill_grade as $k=>$v){
            $data['id_'] = $v['id_'];
            $data['city_'] = $city;
            $data['title_'] = $v['title_'];
            $data['sub_title'] = $v['sub_title'];
            Db::name('skill_paper')->insert($data);
            $chapter_param['token'] = $token;
            $chapter_param['cid'] = $v['id_'];
            $skill_chapter = http_post_json('https://api.1217xc.com/exam2Weixin/getSkillDetail', $chapter_param);
            $skill_chapter = json_decode($skill_chapter, true)['data']['skilllist'];

            foreach ($skill_chapter as $kk=>$vv){
                $data1['id_'] = $vv['id_'];
                $data1['skill_paper_id'] = $v['id_'];
                $data1['title_'] = $vv['title_'];
                $data1['content_'] = $vv['content_'];
                $data1['type_'] = $vv['type_'];
                $data1['city_'] = $city;
                Db::name('skill_chapter')->insert($data1);
            }
        }
        //获取该城市的所有的技巧章节
        $chapter = Db::name('skill_chapter')
            ->where('city_',$city)
            ->select()->toArray();
        //通过技巧等级获取技巧章节
        foreach ($chapter as $k=>$v) {
            $output->writeln("执行到第".($k+1).'章开始');
            $url = "https://api.1217xc.com/exam2Weixin/getExamListByType";
            $param['token'] = $token;
            if ($v['city_'] == '0') {
                $param['city'] = 'national';
            } else {
                $param['city'] = $v['city_'];
            }
            $param['chapter'] = $v['id_'];
            $param['type'] = 'zxlx';
            $param['location'] = '上海';
            $result = http_post_json($url, $param);
            $result = json_decode($result, true);
            if (isset($result['data']['list'])) {
                $list = $result['data']['list'];
                $temp = [];
                foreach ($list as $kk=>$vv){
                    //通过接口获取对应的题目详情
                    $url1 = 'https://api.1217xc.com/exam2Weixin/getQuestionSkill';
                    $param1['token'] = $token;
                    $param1['qid'] = $vv['id_'];
                    $param1['city'] = '杭州';
                    $param1['skill_id'] = $v['id_'];
                    $result1 = http_post_json($url1,$param1);
                    $result1 = json_decode($result1,true);
                    $data = $result1['data']['skill'];
                    $temp[$kk]['skill_paper_id'] = $v['skill_paper_id'];
                    $temp[$kk]['skill_chapter_id'] = $v['id_'];
                    $temp[$kk]['qid'] = $vv['id_'];
                    $temp[$kk]['title_'] = $data['title_'];
                    $temp[$kk]['content_'] = $data['content_'];
                    $temp[$kk]['skill_desc'] = $data['skill_desc'];
                    $temp[$kk]['city_'] = $v['city_'];
                    //$output->writeln("执行到第".($kk+1).'题');
                }
                Db::name('skill_chapter_question')->insertAll($temp);
                $output->writeln("执行第".($k+1).'章结束');
            }
        }
        $output->writeln("计划任务执行完成");
    }
}